package org.opens.hutool.convert;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.opens.lambda.UserEntity;

import java.util.Arrays;

public class CustomMyConvert extends Convert {

    public static JSONObject toJSONObject(Object param) throws ConvertJSONObjectException {
        String json = JSON.toJSONString(param);
        if (!JSONObject.isValidObject(json)) {
            throw new ConvertJSONObjectException("被转换对象不满足json要求");
        }
        return JSONObject.parseObject(json);
    }

    public static JSONArray boxToJSONArray(JSONObject... param) {
        JSONArray array = new JSONArray();
        array.addAll(Arrays.asList(param));
        return array;
    }

    /**
     * 功能:
     * <p>验证转换方法是否可用</p>
     */
    @Test
    public void test30() throws ConvertJSONObjectException {
        UserEntity randomUser = UserEntity.getRandomUser();
        System.out.println(toJSONObject(randomUser));
    }

    /**
     * 功能:
     * <p>测试装箱JSONObject为JSONArray是否会报错</p>
     */
    @Test
    public void test51() throws ConvertJSONObjectException {
        UserEntity randomUser1 = UserEntity.getRandomUser();
        UserEntity randomUser2 = UserEntity.getRandomUser();
        UserEntity randomUser3 = UserEntity.getRandomUser();
        JSONObject object1 = toJSONObject(randomUser1);
        JSONObject object2 = toJSONObject(randomUser2);
        JSONObject object3 = toJSONObject(randomUser3);
        JSONArray array = boxToJSONArray(object1, object2, object3);
        System.out.println(array);
    }

    /**
     * 输出:
     * <pre>
     *      [{"name":"s1","id":1},{"$ref":"$[0]"},{"$ref":"$[0]"}]
     *      [{"name":"s1","id":1},{"$ref":"$[0]"},{"$ref":"$[0]"}]
     * </pre>
     * 说明:
     * <pre>
     *     1. fastjson的JSONArray在添加同一个对象多次的情况下会序列化错误;
     *     2. 虽然JSONArray序列化会报错, 但是仍旧可以从其中获取值且不会报错;
     * </pre>
     */
    @Test
    public void test67() {
        JSONObject object = new JSONObject();
        object.put("id", 1);
        object.put("name", "s1");

        JSONArray objects = new JSONArray();
        objects.add(object);
        objects.add(object);
        objects.add(object);

        System.out.println(objects);
        System.out.println(objects.toJSONString());

        JSONObject jsonObject = objects.getJSONObject(2);
        System.out.println(jsonObject);
        System.out.println(jsonObject.get("id"));
    }

    /**
     * 说明:
     * <pre>
     *     1. hutool提供的json工具也可以很方便的实现json转换, 且在添加同一个对象多次的情况下不会报错.
     * </pre>
     */
    @Test
    public void test98() {
        cn.hutool.json.JSONObject jsonObject = new cn.hutool.json.JSONObject();
        jsonObject.put("id", 1);
        jsonObject.put("name", "s1");

        cn.hutool.json.JSONArray jsonArray = new cn.hutool.json.JSONArray();
        jsonArray.add(jsonObject);
        jsonArray.add(jsonObject);
        jsonArray.add(jsonObject);

        System.out.println(jsonArray);

        cn.hutool.json.JSONObject jsonObject1 = jsonArray.getJSONObject(2);
        System.out.println(jsonObject1);
    }

}

class ConvertJSONObjectException extends Exception {

    public ConvertJSONObjectException(String message) {
        super(message);
    }

}
